package client;

import java.io.*;
import java.net.Socket;
import java.util.Vector;

import model.*;
import com.google.gson.Gson;


//此类用于获取客户端的键盘输入，并且将输入转发到服务端
public class MessageSender {
    private DataOutputStream dos;        //dos用以来连接MessageForward类的输入流
    private boolean flag=true;
    private int Userid;
    
    //构造函数
    public MessageSender(DataOutputStream dos,int userid)
    {
    	try
    	{
    	 this.dos = dos;
    	 Userid = userid;
    	}catch(Exception e)
    	{
    		e.printStackTrace();
    	}
    }
    
    //向服务端发送键盘输入的信息
    public void Send(String str,int roomId)
    {
    	try
    	{
    		Gson gson = new Gson();
    		Message mes = new Message();
    		mes.setText(str);
    		mes.setRoomID(roomId);
    		mes.setType(MsgType.COMMON_MESSAGE);
    		mes.setUserID(Userid);
    		
    		String json = gson.toJson(mes);
    		dos.writeUTF(json);
    		dos.flush();
    	}catch(IOException e)
    	{
    		flag = false;
    		e.getMessage();
    	}
    }
    
    // 向服务端发送与聊天机器人的信息
    public void MachineSend(String msg, int isMachine) {
    	try
    	{
    		Gson gson = new Gson();
    		Message mes = new Message();
    		mes.setText(msg);
    		mes.setRoomID(isMachine);
    		mes.setType(MsgType.MACHINE_MESSAGE);
    		mes.setUserID(Userid);
    		
    		String json = gson.toJson(mes);
    		dos.writeUTF(json);
    		dos.flush();
    	}catch(IOException e)
    	{
    		flag = false;
    		e.getMessage();
    	}
    }
    
}
